import re

import setuptools

package_name = "baotool"
VERSION = "1.0.1"


def curr_version():
    return VERSION
    # 方法1：通过文件临时存储版本号
    # with open('VERSION', encoding='utf-8') as f:
    #     version_str = f.read()
    #     return str(version_str).strip()

    # 方法2：从官网获取版本号
    # url = f"https://pypi.org/project/{package_name}/"
    # response = requests.get(url)
    # soup = BeautifulSoup(response.content, "html.parser")
    # latest_version = soup.select_one(".release__version").text.strip()
    # return str(latest_version)


def get_version():
    # 从版本号字符串中提取三个数字并将它们转换为整数类型
    match = re.search(r"(\d+)\.(\d+)\.(\d+)", curr_version())
    major = int(match.group(1))
    minor = int(match.group(2))
    patch = int(match.group(3))

    # 对三个数字进行加一操作
    patch += 1
    if patch > 9:
        patch = 0
        minor += 1
        if minor > 9:
            minor = 0
            major += 1
    new_version_str = f"{major}.{minor}.{patch}"
    return new_version_str


def upload():
    with open("README.md", "r", encoding='utf-8') as fh:
        long_description = fh.read()
    required = []
    try:
        with open('requirements.txt', encoding='utf-8') as f:
            required = f.read().splitlines()
    except:
        print(f'ERROR: open `requirements.txt` failed')

    setuptools.setup(
        name=package_name,
        version=curr_version(),
        author="Nisus Liu",  # 作者名称
        author_email="609069481@qq.com", # 作者邮箱
        description="BaoTool (宝兔), 个人积累的 python 工具库", # 库描述
        long_description=long_description,
        long_description_content_type="text/markdown",
        url="https://pypi.org/project/bao-tool/", # 库的官方地址
        packages=setuptools.find_packages(),
        data_files=["requirements.txt"], # yourtools库依赖的其他库
        classifiers=[
            "Programming Language :: Python :: 3",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
        python_requires='>=3.6',
        install_requires=required,
        include_package_data=True,
        # package_data={
        #     "": ['requirements.txt', "README.md", 'VERSION'],  # 指定哪些文件应该被包含
        # },
    )


def write_now_version():
    print("Current VERSION:", get_version())
    with open("VERSION", "w", encoding='utf-8') as version_f:
        version_f.write(get_version())


def main():
    try:
        upload()
        print("Upload success , Current VERSION:", curr_version())
    except Exception as e:
        raise Exception("Upload package error", e)


if __name__ == '__main__':
    main()
